//
//  GalleryView.m
//  ios_news
//
//  Created by Han on 14-7-6.
//  Copyright (c) 2014年 ucoder. All rights reserved.
//

#import "GalleryView.h"

@implementation GalleryView

- (id) initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
        
        [self drawView];
    }
    
    return self;

}


- (void) drawView
{
    mGalleryScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 160)];
    mGalleryScrollView.showsHorizontalScrollIndicator = NO;
    mGalleryScrollView.pagingEnabled = YES;
    mGalleryScrollView.delegate = self;
    
    NSArray *imgArray = @[@"2.png", @"0.png", @"1.png", @"2.png", @"0.png"];
    
    
    for (int i = 0; i < 5; i++) {
        UIImageView *img = [[UIImageView alloc]initWithImage:[UIImage imageNamed:[imgArray objectAtIndex:i]]];
        [img setFrame:CGRectMake(320 * i, 0, 320, 160)];
        [mGalleryScrollView addSubview:img];
    }
    
    // 必须给出内容的实际大小，滚动视图才知道滚动的范围
    mGalleryScrollView.contentSize = CGSizeMake(320 * 5, 160);
    
    mCurrentPage = 1;
    
    // 设置显示区域
    CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
    [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    
    
    [self addSubview:mGalleryScrollView];
    
    
    mPageControl = [[UIPageControl alloc] init];
    mPageControl.numberOfPages = 3;
    mPageControl.currentPage = 0;
    mPageControl.enabled = YES;
    mPageControl.currentPageIndicatorTintColor = MAIN_COLOR;
    mPageControl.pageIndicatorTintColor = [UIColor whiteColor];
    mPageControl.center = CGPointMake(160, self.frame.size.height*0.9);
    
    [self addSubview:mPageControl];
    
    
    // 循环播放
    [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(scrollToNextPage:) userInfo:nil repeats:YES];
}

// 自动翻页
- (void) scrollToNextPage:(id)sender {
    
    CGRect visableImage = CGRectMake((mCurrentPage+1) * mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
    [mGalleryScrollView scrollRectToVisible:visableImage animated:YES];
    
    //NSLog(@"%ld", (long)[mPageControl currentPage]);
    /*
    // 跟手动滑动的原理竟然不一样，很奇怪
    if ([mPageControl currentPage] == 2) {
        
        [mPageControl setCurrentPage:0];
        
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
        
    } else {
        CGRect visableImage = CGRectMake(([mPageControl currentPage]+2) * mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:YES];
        [mPageControl setCurrentPage:([mPageControl currentPage]+1)];
    }
     */
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    NSInteger tempCurrentPage = (NSInteger) (scrollView.contentOffset.x/320);
    if (mCurrentPage == tempCurrentPage) {
        return;
    }
    
    mCurrentPage = tempCurrentPage;
    if (mCurrentPage == 4) {
        // 最后一页 设置当前页为0
        [mPageControl setCurrentPage:0];
        
        // 向后翻页，隐蔽的覆盖第一页
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else if (mCurrentPage == 0) {
        [mPageControl setCurrentPage:2];
        
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width * 3, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else {
        [mPageControl setCurrentPage: mCurrentPage - 1];
    }
}

/*
- (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    //NSLog(@"+++++++");
    //CGPoint contentOffset = scrollView.contentOffset;
    mCurrentPage = (NSInteger) (scrollView.contentOffset.x/320);
    
    if (mCurrentPage == 4) {
        // 最后一页 设置当前页为0
        [mPageControl setCurrentPage:0];
        
        // 向后翻页，隐蔽的覆盖第一页
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else if (mCurrentPage == 0) {
        [mPageControl setCurrentPage:2];
        
        CGRect visableImage = CGRectMake(mGalleryScrollView.frame.size.width * 3, 0.0, mGalleryScrollView.frame.size.width, mGalleryScrollView.frame.size.height);
        [mGalleryScrollView scrollRectToVisible:visableImage animated:NO];
    } else {
        [mPageControl setCurrentPage: mCurrentPage - 1];
    }
}
 */
 
@end
